import globalVisionEntertainment.nba.Database;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.sql.*;
import java.util.Arrays;
import java.util.List;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

public abstract class SheetFrame extends JFrame
{

	protected final DefaultTableModel tableModel;
	protected final JTable table;
	protected final CustomCellRenderer highLightColumnRenderer;
	private final boolean hasCheckBox;

	public SheetFrame(boolean hasCheckBox)
	{
		if (hasCheckBox)
			tableModel = new CheckboxTableModel();
		else
			tableModel = new DefaultTableModel();
		this.hasCheckBox = hasCheckBox;

		highLightColumnRenderer = new CustomCellRenderer();

		table = new JTable(tableModel);
	}

	public void windowOpened(WindowEvent e)
	{
		// contextMenu.add("Show last 10 games");
		// contextMenu.add("Show last 5 games");

		try
		{
			loadData();
		}
		catch (IOException e1)
		{
			JOptionPane.showMessageDialog(this, e1.getMessage());
			System.exit(1);
		}

		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		TableColumnModel columnModel = table.getColumnModel();
		List<String> highlightedColumns = Arrays.asList(Settings.getHighLightedColumns());

		for (int i = 0; i < columnModel.getColumnCount(); i++)
		{

			String columnName = tableModel.getColumnName(i);
			if (highlightedColumns.contains(columnName))
				columnModel.getColumn(i).setCellRenderer(highLightColumnRenderer);

			columnModel.getColumn(i).setPreferredWidth(ColumnWidth.getWidth(columnName));
		}

	}

	protected abstract void loadData() throws IOException;
}